import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_easyrefresh/material_header.dart';
import 'package:flutter_wan_android/bean/project_list_bean.dart';
import 'package:flutter_wan_android/blocs/base/bloc_provider.dart';
import 'package:flutter_wan_android/blocs/project_bloc.dart';
import 'package:flutter_wan_android/res/colors.dart';
import 'package:flutter_wan_android/widgets/app_bar_widget.dart';
import 'package:flutter_wan_android/widgets/empty_widget.dart';
import 'package:flutter_wan_android/widgets/home_widgets.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

class NewProjectPage extends StatefulWidget {

  @override
  State<StatefulWidget> createState() {
    return _NewProjectState();
  }
}


class _NewProjectState extends State{
  RefreshController _refreshController = RefreshController(initialRefresh: false);
  ProjectBloc _bloc;
  EasyRefreshController _refreshControllerE;
  int _pageIndex = 0;


  @override
  void initState() {
    super.initState();
    _refreshControllerE = EasyRefreshController();

    _bloc = BlocProvider.of<ProjectBloc>(context);

    Future.delayed(Duration(seconds: 2),
            ()=> _bloc.getHomeProjectList(_pageIndex));


  }


  @override
  void dispose() {
    super.dispose();
    _bloc.allData.clear();
    _bloc.dispose();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonAppBar.buildAppBar(context, "项目"),
      body: StreamBuilder<List<ProjectItem>>(
          stream: _bloc.stream,
          builder: (context, AsyncSnapshot<List<ProjectItem>> snapshot) {

            if(_pageIndex==0){
              _refreshController.refreshCompleted();
            }else{
              _refreshController.loadComplete();
            }

            if(_bloc.allData.length==0 && _pageIndex==0){
              return LoadingView();
            }

            return buildSmartRefresher();
          }),
    );
  }



  SmartRefresher buildSmartRefresher() {
    return SmartRefresher(
      child: ListView.separated(itemBuilder: (context,index){
        return ProjectItemView(_bloc.allData.elementAt(index));
      }, separatorBuilder: (context, index) {
        return Container(
          height: 0.5,
          color: Colors.transparent,
        );
      }, itemCount: _bloc.allData.length),
      controller: _refreshController,
      enablePullUp: true,
      footer: ClassicFooter(
        loadStyle: LoadStyle.ShowAlways,
        completeDuration: Duration(milliseconds: 500),
      ),
      header:WaterDropMaterialHeader(
        backgroundColor: XColors.refreshStyleColor,
      ),
      onRefresh: () async {
        _refresh();
      },
      onLoading: () async {
        _loading();


      },
    );
  }

  void _loading() {
    ++_pageIndex;
    _bloc.getHomeProjectList(_pageIndex);
  }

  void _refresh() {
    _pageIndex = 0;
    _bloc.getHomeProjectList(_pageIndex);
  }




  @Deprecated('不使用')
  EasyRefresh buildEasyRefresh(BuildContext context, AsyncSnapshot<List<ProjectItem>> snapshot) {
    return EasyRefresh.custom(
        header: MaterialHeader(),
//                footer: MaterialFooter(),
        controller: _refreshControllerE,
        firstRefresh: true,
        firstRefreshWidget: LoadingView.buildLoading(context),
        onRefresh: () async {
          _pageIndex = 0;
          _bloc.getHomeProjectList(_pageIndex);

        },
        onLoad: () async {

          ++_pageIndex;
          _bloc.getHomeProjectList(_pageIndex);

        },
        slivers: <Widget>[
          SliverList(
            delegate: SliverChildBuilderDelegate(
                  (context, index) {
                if (snapshot.hasData) {
                  return ProjectItemView(
                      _bloc.allData.elementAt(index));
                } else {

                  return Container(
                    height: 0,
                  );
                }
              },
              childCount: _bloc.allData.length,
            ),
          ),
        ]);
  }


}
